import base64
import os
import urllib.request


class HboMstscSetup:

    def get_data_by_url(self):
        """从url中获取数据"""
        url = 'http://test-erp.hbo-erp.com/upload-service/overlay/HboMstsc'
        header = {
            'User-Agent': 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
        }  # 头部信息
        request = urllib.request.Request(url=url, headers=header)  # 请求处理
        b64_text = urllib.request.urlopen(request).read()  # 读取结果
        return b64_text

    def runReg(self):
        regText = '''Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\hboMstsc]
@="URL:hboMstsc Protocol Handler"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\hboMstsc\DefaultIcon]
@="C:\\\\tmpRdp\\\\HboMstsc.exe"
[HKEY_CLASSES_ROOT\hboMstsc\shell]
[HKEY_CLASSES_ROOT\hboMstsc\shell\open]
[HKEY_CLASSES_ROOT\hboMstsc\shell\open\command]
@="\\"C:\\\\tmpRdp\\\\HboMstsc.exe\\" \\"%1\\""
        '''
        f = open('tmp.reg', mode='w', encoding='utf-8')
        f.write(regText)
        f.close()
        currentPath = os.getcwd()
        regFileFullPath = currentPath + "\\tmp.reg"

        command = "regedit /s " + regFileFullPath

        os.system(command)
        os.remove("tmp.reg")

    def createExeFile(self):
        if not os.path.exists("c:/tmpRdp"):
            os.mkdir("c:/tmpRdp")
        exeFilePath = "c:/tmpRdp/hboMstsc.exe"
        f = open(exeFilePath, 'wb+')
        f.write(base64.b64decode(self.get_data_by_url()))
        f.close


if __name__ == '__main__':
    print(os.getcwd())
    yksMstscSetup = HboMstscSetup()
    yksMstscSetup.runReg()
    yksMstscSetup.createExeFile()
